﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Text.RegularExpressions;

namespace FPP.Models
{
    public partial class User : EntityObject, IValidate
    {
        #region Primitive Properties

        public virtual int Id
        {
            get;
            set;
        }

        [Required(ErrorMessage = "First Name Required")]
        [Display(Name = "First Name")]
        public virtual string FirstName
        {
            get;
            set;
        }

        [Required(ErrorMessage = "Last Name Required")]
        public virtual string LastName
        {
            get;
            set;
        }

        public virtual string MiddleName
        {
            get;
            set;
        }

        public virtual string Suffix
        {
            get;
            set;
        }

        [Required(ErrorMessage = "Email Required")]
        [RegularExpression("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")]
        public virtual string Email
        {
            get;
            set;
        }

        public virtual string Password
        {
            get;
            set;
        }

        public virtual string LoginToken
        {
            get;
            set;
        }

        public virtual Nullable<System.DateTime> LoginExp
        {
            get;
            set;
        }

        public virtual string role
        {
            get;
            set;
        }

        public virtual Nullable<long> fb_user_id
        {
            get;
            set;
        }

        public virtual string fb_oauth_token
        {
            get;
            set;
        }

        [DisplayName("Display Facebook Avatar")]
        public virtual Nullable<bool> fbDisplayAvatar
        {
            get;
            set;
        }

        #endregion

        #region Custom Properties

        public string DisplayName
        {
            get
            {
                return string.Format("{0} {1}", FirstName, LastName);
            }
        }

        #endregion

        public bool Validate(ChangeAction action)
        {
            switch (action)
            {
                case ChangeAction.Insert:
                    if (string.IsNullOrEmpty(FirstName)) return false;
                    if (string.IsNullOrEmpty(Email)) return false;
                    var regex = new Regex("^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
                    if (!regex.IsMatch(Email)) return false;
                    break;
            }

            return true;
        }
    }
}